package com.aier.admin.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.common.AdminFinalPageSize;
import com.aier.admin.service.IAdminEntranceService;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.entrance.common.EntranceICFinalData;
import com.aier.entrance.common.EntranceServerData;
import com.aier.entrance.entity.EntranceDevice;

/**
 * 后台管理门禁设备的管理 包含添加门禁设备信息(entradd),删除门禁信息
 * (entrdelete),修改门禁信息(entrupdate),显示门禁信息(entrlist)
 * 
 * @author sun
 * 
 */
@Scope("prototype")
@Component("adminEntrCreateAction")
public class AdminEntrCreateAction extends AdminBaseAction {
    @Resource(name = "adminEntranceService")
    private IAdminEntranceService adminEntranceService;
    private String sn;// 页面传入设备sn
    private Short state;// 设备状态
    private String des;// 设备描述
    private Long eid;// 门禁设备id
    private Short vdstate;// 是否开始拍照
    private Short supplier;// 设备厂家
    private Integer pid;
    private EntranceDevice entranceDevice;

    private Map<Short, String> es = EntranceServerData.entranceSupplier;
    private List<EntranceDevice> enlist;
    private List<EntranceDevice> alreadyassignlist;
    // 当前页
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    /**
     * 
     * @return s 0:失败 2:数据非法 1:成功
     */
    private int s;

    private static final long serialVersionUID = 1L;

    @Override
    public String execute() {
        return SUCCESS;
    }

    /**
     * 新增门禁信息
     * 
     * @return
     */
    public String entradd() {
        EntranceDevice en = new EntranceDevice();
        en.setSn(sn);
        en.setDes(des);
        if (null == state) {
            s = 0;
            return SUCCESS;
        }
        if (0 == state) {
            en.setState(EntranceICFinalData.ENTRANCE_STATE_OFF);
        } else if (Constant.num_10 == state) {
            en.setState(EntranceICFinalData.VIDEO_STATE_ON);
        } else {
            s = 2;
            return SUCCESS;
        }
        if (vdstate == Constant.num_11) {
            en.setVdstate(EntranceICFinalData.ENTRANCE_VDSTATE_ALL);
        } else if (vdstate == 0) {
            en.setVdstate(EntranceICFinalData.ENTRANCE_VDSTATE_OFF);
        } else if (vdstate == Constant.num_10) {
            en.setVdstate(EntranceICFinalData.ENTRANCE_VDSTATE_IN);
        } else if (vdstate == 1) {
            en.setVdstate(EntranceICFinalData.ENTRANCE_VDSTATE_OUT);
        } else {
            s = 2;
            return SUCCESS;
        }

        en.setSupplier(supplier);

        s = adminEntranceService.insertEntrance(en);

        return SUCCESS;

    }

    /**
     * 显示门禁列表
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String entrlist() {
        if (page <= 0) {
            page = 1;
        }
        Map<String, Object> map = null;
        map = adminEntranceService.searchEntranceDevice(page, AdminFinalPageSize.ENTRANCE_DEVICE_PAGESIZE);
        enlist = (List<EntranceDevice>) map.get(ICommonDAO.OBJ_LIST);
        if (null == enlist) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s = 1;
        return SUCCESS;
    }

    /**
     * 根据eid删除门禁信息
     * 
     * @return
     */
    public String entrdelete() {

        s = adminEntranceService.updateEntranceByid(eid);

        return SUCCESS;
    }

    /**
     * 根据eid修改门禁信息
     * 
     * @return
     */
    public String entrupdate() {
        if (null == state) {
            s = 0;
            return SUCCESS;
        }
        if (0 == state) {
            state = EntranceICFinalData.ENTRANCE_STATE_OFF;
        } else if (Constant.num_10 == state) {
            state = EntranceICFinalData.ENTRANCE_STATE_ON;
        } else {
            s = 2;
            return SUCCESS;
        }
        if (vdstate == Constant.num_11) {
            vdstate = EntranceICFinalData.ENTRANCE_VDSTATE_ALL;
        } else if (vdstate == 0) {
            vdstate = EntranceICFinalData.ENTRANCE_VDSTATE_OFF;
        } else if (vdstate == Constant.num_10) {
            vdstate = EntranceICFinalData.ENTRANCE_VDSTATE_IN;
        } else if (vdstate == 1) {
            vdstate = EntranceICFinalData.ENTRANCE_VDSTATE_OUT;
        } else {
            s = 2;
            return SUCCESS;
        }

        s = adminEntranceService.updateByeid(eid, sn, state, des, vdstate,
                supplier);

        return SUCCESS;
    }

    /**
     * 根据eid查询某条门禁记录
     * 
     * @return
     */
    public String searchentr() {
        entranceDevice = adminEntranceService.searchByeid(eid);

        return SUCCESS;
    }

    /**
     * 根据eid分派门禁设备
     * 
     * @return
     */
    public String updatepid() {
        entranceDevice = adminEntranceService.searchByeid(eid);
        if (entranceDevice.getPid() == 0) {
            if (pid != null) {
                s = adminEntranceService.updatepidByeid(pid, eid);
            } else {
                s = 0;
            }
        } else {
            s = 0;
        }
        return SUCCESS;
    }

    /**
     * 根据eid回收门禁设备
     * 
     * @return
     */
    public String updatenullpid() {

        s = adminEntranceService.updatepidByeid(null, eid);
        return SUCCESS;
    }

    /**
     * 查出所有未分配的门禁设备
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String selectUnassignEntrance() {
        if (page <= 0) {
            page = 1;
        }
        Map<String, Object> map = null;
        map = adminEntranceService.selectUnassign(page, AdminFinalPageSize.UNALLOCATED_ENTRANCE_PAGESIZE);
        enlist = (List<EntranceDevice>) map.get(ICommonDAO.OBJ_LIST);
        if (null == enlist) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s = 1;
        return SUCCESS;
        
    }

    /**
     * 查出所有已分配的门禁设备
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String selectAlreadyassignEntrance() {
        if (page <= 0) {
            page = 1;
        }
        Map<String, Object> map = null;
        map = adminEntranceService.selectAlreadyassign(page, AdminFinalPageSize.ALLOCATION_DEVICE_PAGESIZE);
        alreadyassignlist = (List<EntranceDevice>) map.get(ICommonDAO.OBJ_LIST);
        if (null == alreadyassignlist) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s = 1;
        return SUCCESS;

    }

    public void setAdminEntranceService(
            IAdminEntranceService adminEntranceService) {
        this.adminEntranceService = adminEntranceService;
    }

    public int getS() {
        return s;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public void setVdstate(Short vdstate) {
        this.vdstate = vdstate;
    }

    public void setSupplier(Short supplier) {
        this.supplier = supplier;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public void setState(short state) {
        this.state = state;
    }

    public Map<Short, String> getEs() {
        return es;
    }

    public Short getState() {
        return state;
    }

    public void setState(Short state) {
        this.state = state;
    }

    public List<EntranceDevice> getEnlist() {
        return enlist;
    }

    public void setEnlist(List<EntranceDevice> enlist) {
        this.enlist = enlist;
    }

    public String getSn() {
        return sn;
    }

    public String getDes() {
        return des;
    }

    public Short getVdstate() {
        return vdstate;
    }

    public Short getSupplier() {
        return supplier;
    }

    public void setS(int s) {
        this.s = s;
    }

    public Long getEid() {
        return eid;
    }

    public void setEid(Long eid) {
        this.eid = eid;
    }

    public EntranceDevice getEntranceDevice() {
        return entranceDevice;
    }

    public void setEntranceDevice(EntranceDevice entranceDevice) {
        this.entranceDevice = entranceDevice;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public List<EntranceDevice> getAlreadyassignlist() {
        return alreadyassignlist;
    }

    public void setAlreadyassignlist(List<EntranceDevice> alreadyassignlist) {
        this.alreadyassignlist = alreadyassignlist;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }

}
